package com.person.android.widgets;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.person.android.formatts.R;

/**
 * @autor zhaokang.
 * @time on 2015/12/10.
 */
public class FileSelectAlertDialog extends Dialog {

    protected FileSelectAlertDialog(Context context) {
        super(context);
    }

    public FileSelectAlertDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private String title;
        private String message;
        private Context context;
        private View contentView;
        private String positiveBtnTxt;
        private String negativeBtnTxt;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setTitle(String tile) {
            this.title = tile;
            return this;
        }

        public Builder setMessage(int messageID) {
            this.message = (String) context.getText(messageID);
            return this;
        }

        public Builder setTitle(int tileID) {
            this.title = (String) context.getText(tileID);
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveBtnTxt = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeBtnTxt, DialogInterface.OnClickListener listener) {
            this.negativeBtnTxt = (String) context.getText(negativeBtnTxt);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveBtnTxt = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeBtnTxt, DialogInterface.OnClickListener listener) {
            this.negativeBtnTxt = negativeBtnTxt;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public FileSelectAlertDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final FileSelectAlertDialog dialog = new FileSelectAlertDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_file_select_layout, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            if (positiveBtnTxt != null) {
                ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveBtnTxt);
                if (positiveButtonClickListener != null) {
                    (layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
            }
            if (negativeBtnTxt != null) {
                ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeBtnTxt);
                if (negativeButtonClickListener != null) {
                    (layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            }else if (contentView != null) {
                ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}
